import Cookies from 'js-cookie'

const TokenKey = 'ruifox_admin_token'
const routerKey = 'ruifox_user_router'
const menuKey = 'ruifox_user_menu'
const userInfoKey = 'ruifox_user_info'
export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token, { expires: 2 })
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}
export function setRouter(routes) {
  routes = JSON.stringify(routes)
  return localStorage.setItem(routerKey, routes)
}
export function getRouter() {
  return JSON.parse(localStorage.getItem(routerKey))
}
export function removeRouter() {
  return localStorage.removeItem(routerKey)
}
export function setMenus(menus) {
  menus = JSON.stringify(menus)
  return localStorage.setItem(menuKey, menus)
}
export function getMenus() {
  return JSON.parse(localStorage.getItem(menuKey))
}
export function removeMenus() {
  return localStorage.removeItem(menuKey)
}
export function setLoginState(state) {
  return Cookies.set('ruifox_loginState', state, { expires: 2 })
}
export function getLoginState() {
  return Cookies.get('ruifox_loginState')
}
export function setRefreshToken(token) {
  return Cookies.set('ruifox_refresh_token', token)
}
export function getRefreshToken() {
  return Cookies.get('ruifox_refresh_token')
}
export function getThemeColor() {
  return Cookies.get("ruifox_admin_theme");
}
export function setThemeColor(themeColor) {
  return Cookies.set('ruifox_admin_theme', themeColor)
}
export function setUserInfo(info) {
  info = JSON.stringify(info)
  return localStorage.setItem(userInfoKey, info)
}
export function getUserInfo() {
  return JSON.parse(localStorage.getItem(userInfoKey))
}
export function setSiteGroup(sites) {
  let _sites = JSON.stringify(sites);
  return localStorage.setItem("ruifox_site_group", _sites);
}
export function getSiteGroup() {
  return JSON.parse(localStorage.getItem("ruifox_site_group"));
}
export function setCurrentSite(site) {
  site = JSON.stringify(site)
  return localStorage.setItem("ruifox_current_site", site);
}
export function getCurrentSite() {
  const site = localStorage.getItem("ruifox_current_site");
  if(!site || site == 'undefined'){
    return null
  }
  return JSON.parse(localStorage.getItem("ruifox_current_site"));
}